﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient


''' <summary>
''' Interaction logic for SettingsWindow.xaml
''' </summary>
Partial Public Class SettingsWindow
    Inherits Window
    ' These are global object which will be use to communicate with the 
    ' database whithin this class
    Private objSqlDatabase As New SqlDatabase(GlobalClass._ConStr)
    Private objDatabaseClass As New DatabaseClass(GlobalClass._ConStr)
    Private logoPath As String = ""

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub CloseCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        Me.Close()
    End Sub

    Private Sub Window_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        Me.DragMove()
    End Sub

    Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim dt As DataTable = GlobalClass.GetRestaurantInfo()
        If dt IsNot Nothing Then
            If dt.Rows.Count > 0 Then
                txtRestaurantName.Text = dt.Rows(0)("RestaurantName").ToString()
                txtPhoneNumber.Text = dt.Rows(0)("PhoneNumber").ToString()
                txtWebsite.Text = dt.Rows(0)("WebsiteURL").ToString()
                txtEmail.Text = dt.Rows(0)("Email").ToString()
                txtAddress.Text = dt.Rows(0)("Address").ToString()

                Try
                    If dt.Rows(0)("Logo") IsNot DBNull.Value Then
                        Dim arr As Byte() = DirectCast(dt.Rows(0)("Logo"), Byte())
                        Dim ms As New MemoryStream(arr)
                        Dim bitmap As New BitmapImage()
                        bitmap.BeginInit()
                        bitmap.StreamSource = ms
                        bitmap.EndInit()
                        imgLogo.Source = bitmap
                    End If
                Catch
                End Try
            End If
        End If
    End Sub

    Private Sub btnBrowseLogo_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim ofd As New System.Windows.Forms.OpenFileDialog()
        ofd.Filter = "Image files|*.jpg;*.png;*.gif"
        Dim dgr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
        If dgr = System.Windows.Forms.DialogResult.OK Then
            Dim bitmap As New BitmapImage(New Uri(ofd.FileName))
            If bitmap.Width <= 80 AndAlso bitmap.Height <= 80 Then
                imgLogo.Source = bitmap
                logoPath = ofd.FileName
            Else
                MessageBox.Show("Logo image should have the size of 80*80 or less!", "", MessageBoxButton.OK, MessageBoxImage.[Error])
            End If
        End If
    End Sub

    Private Sub btnSaveChanges_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Cursor = Cursors.Wait
        If txtRestaurantName.Text <> "" AndAlso imgLogo.Source IsNot Nothing Then
            Dim spName As String = "Update_Settings"
            If objDatabaseClass.CheckConnection() Then
                Dim spParams As Object() = New Object(5) {}
                spParams(0) = txtRestaurantName.Text
                spParams(1) = txtAddress.Text
                spParams(2) = txtPhoneNumber.Text
                spParams(3) = txtWebsite.Text
                spParams(4) = txtEmail.Text
                If logoPath <> "" Then
                    Dim fsLogo As New FileStream(logoPath, FileMode.Open, FileAccess.Read)
                    Dim objFileInfoLogo As New FileInfo(logoPath)
                    Dim brLogo As New BinaryReader(fsLogo)
                    Dim lengthLogo As Long = objFileInfoLogo.Length
                    Dim arrImgLogo As Byte() = New Byte(Convert.ToInt32(lengthLogo) - 1) {}
                    arrImgLogo = brLogo.ReadBytes(Convert.ToInt32(lengthLogo))
                    fsLogo.Flush()
                    brLogo.Close()
                    fsLogo.Close()
                    spParams(5) = arrImgLogo
                Else
                    spParams(5) = DBNull.Value
                End If
                Try
                    Dim result As Integer = objSqlDatabase.ExecuteNonQuery(spName, spParams)
                    If result > 0 Then
                        MessageBox.Show("Settings Saved Successfully!")
                        Me.DialogResult = False
                    End If
                Catch generatedExceptionName As SqlException
                    MessageBox.Show(ErrorMessages.[Default].UnhandledException, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
                End Try
            Else
                MessageBox.Show(ErrorMessages.[Default].NoConnection, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
            End If
        Else
            MessageBox.Show(String.Format(ErrorMessages.[Default].FieldEmpty, "Restaurant Name") & vbLf & String.Format(ErrorMessages.[Default].FieldEmpty, "Logo"), "", MessageBoxButton.OK, MessageBoxImage.[Error])
        End If
        Cursor = Cursors.Arrow
    End Sub
End Class
